package com.xiaoge.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaoge.annotation.ControllerEndpoint;
import com.xiaoge.entity.EduCourse;
import com.xiaoge.entity.User;
import com.xiaoge.exception.BusinessException;
import com.xiaoge.response.Result;
import com.xiaoge.service.EduCourseService;
import com.xiaoge.service.OssService;
import com.xiaoge.vo.EduCourseVO;
import com.xiaoge.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 *@作者: 冷俊杰
 *@类名: EduCourseController类
 *@创建时间: 2022/10/28 10:31
 *@描述:
 *课程管理
 *@修改原图
 *@修改作者
 *@修改时间
 */
@Api(value = "课程管理", tags = "课程测试")
@RestController
@RequestMapping("/eduCourse")
public class EduCourseController {
    @Resource
    private EduCourseService eduCourseService;
    @Resource
    private OssService ossService;
    /**
     * 功能描述: <br>
     * 〈模糊获取课程信息〉
     * @param current
     * @param size
     * @param courseName
     * @return
     */
    @ApiOperation(value = "课程列表", notes = "模糊查询课程列表")
    @PostMapping("/getEduCourseList")
    public Result getEduCourseList(
            @RequestParam(value = "current",required = true,defaultValue = "1") Integer current,
            @RequestParam(value = "size",required = true,defaultValue = "7") Integer size,
            @RequestParam(value = "courseName",required = true,defaultValue = "")  String courseName){
        Page<EduCourse> page = new Page<>(current,size);
        Page<EduCourse> eduCoursePage = eduCourseService.getEduCourseByPage(page,courseName);
        return Result.ok().data("eduCoursePage",eduCoursePage);
    }

    /**
     * 〈删除课程〉
     * @param id 用户ID
     * @return
     */
    @ControllerEndpoint(exceptionMessage = "删除课程失败", operation = "删除课程")
    @RequiresPermissions({"eduCourse:delete"})
    @ApiOperation(value = "删除课程", notes = "删除课程信息，根据课程ID")
    @DeleteMapping("/delete")
    public Result delete(@RequestParam(value = "id",required = true) Long id) {
        eduCourseService.removeById(id);
        return Result.ok();
    }

    /**
     * 编辑课程，获取信息
     * @param id
     * @return
     */
    @ApiOperation(value = "编辑课程", notes = "获取课程的详情，编辑课程信息")
    @RequiresPermissions({"eduCourse:edit"})
    @GetMapping("/edit/{id}")
    public Result edit(@PathVariable Long id) {
        EduCourse eduCourse = eduCourseService.getById(id);
        if(eduCourse==null){
            throw new BusinessException("要编辑的用户不存在");
        }
        return Result.ok().data("eduCourse",eduCourse);
    }

    /**
     * 修改课程
     * @param eduCourse
     * @return
     */
    @ControllerEndpoint(exceptionMessage = "修改课程失败", operation = "修改课程")
    @ApiOperation(value = "修改课程", notes = "修改课程信息")
    @RequiresPermissions({"eduCourse:update"})
    @PutMapping("/update")
    public Result update(EduCourseVO eduCourse,MultipartFile file) {
        System.out.println(eduCourse.toString());
        eduCourse.setCourseLogo(ossService.upload(file));
        eduCourseService.updateById(eduCourse);
        return Result.ok();
    }

    /**
     * 添加课程
     * @param eduCourse
     * @return
     */
    @ControllerEndpoint(exceptionMessage = "添加课程失败", operation = "添加课程")
    @ApiOperation(value = "添加课程", notes = "添加课程信息")
    @RequiresPermissions({"eduCourse:add"})
    @PostMapping("/save")
    public Result save(EduCourseVO eduCourse,MultipartFile file) {
        eduCourseService.saveEduCourse(eduCourse,file);
        return Result.ok();
    }
}